package net.galluzzo.wave.orthopermubot;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import net.galluzzo.wave.orthopermubot.permutation.MultiplePermutation;
import net.galluzzo.wave.orthopermubot.permutation.Permutation;
import net.galluzzo.wave.orthopermubot.permutation.QwertyKeyboard;
import net.galluzzo.wave.orthopermubot.textviewproc.PrependIfNotPresentTextViewProcessor;
import net.galluzzo.wave.orthopermubot.textviewproc.TextViewProcessor;
import net.galluzzo.wave.orthopermubot.textviewproc.WordBasedTextViewProcessor;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
import com.google.wave.api.RobotMessageBundle;

@SuppressWarnings("serial")
public class OrthopermubotServlet extends AbstractRobotServlet {
	protected static final String COMMENT_STRING =
			"/**\n" +
			" * @author niax0905\n" +
			" *\n" +
			" * To change the template for this generated type comment go to\n" +
			" * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments\n" +
			" */\n";
	
	protected Permutation wordPermutation =
			new MultiplePermutation( new QwertyKeyboard() );
	protected TextViewProcessor textViewProcessor =
			new WordBasedTextViewProcessor( wordPermutation );
	protected Map<String, TextViewProcessor> participantToProcessorMap =
			new HashMap<String, TextViewProcessor>();

	@Override
	public void init( ServletConfig config ) throws ServletException {
		super.init( config );
		participantToProcessorMap.put( "krlaube@googlewave.com",
				new WordBasedTextViewProcessor(
						new MultiplePermutation( new QwertyKeyboard() ) ) );
		participantToProcessorMap.put( "nmcloughlin@googlewave.com",
				new PrependIfNotPresentTextViewProcessor( COMMENT_STRING ) );
	}

	public void processEvents( RobotMessageBundle bundle ) {
		for ( Event e : bundle.getEvents() ) {
			if ( e.getType() == EventType.BLIP_SUBMITTED ) {
				TextViewProcessor processor =
						participantToProcessorMap.get( e.getModifiedBy() );
				if ( processor != null ) {
					processor.processTextView( e.getBlip().getDocument() );
				}
			}
		}
	}
}
